package com.njsoft.bodyawakening.table.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**
 * ViewPager适配器
 */
public class CommonTabPagerAdapter extends FragmentPagerAdapter {

    private final int PAGE_COUNT;
    private List<String> list;
    private TabPagerListener listener;

    /**
     * 单页监听接口
     */
    public interface TabPagerListener{
        Fragment getFragment(int position);
    }

    /**
     * 选择item监听器
     * @param listener
     */
    public void setListener(TabPagerListener listener) {
        this.listener = listener;
    }

    /**
     * 适配器构造方法
     * @param fm  管理器
     * @param list 标题计划
     */
    public CommonTabPagerAdapter(FragmentManager fm, List<String> list) {
        super(fm);
        if (list==null||list.isEmpty()){
            throw new ExceptionInInitializerError("list can't be null or empty");
        }

        this.PAGE_COUNT = list.size();
        this.list = list;
    }

    @Override
    public Fragment getItem(int position) {
       return listener.getFragment(position);
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return list.get(position);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

}
